﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public static class Extend
{

    //public static int ToInt(this Vector3 v)
    //{
    //    return (int)v.x + 128 + (((int)v.y + 128) << 8) + (((int)v.z + 128) << 16);
    //    //return (int)v.x + (((int)v.y ) << 8) + (((int)v.z ) << 16);
    //}

    //public static int ToInt(this Vector3Int v)
    //{
    //    return (v.x + 512) | ((v.y + 512) << 0x0A) | (v.z << 0x14) ;
    //}
    //public static Vector3Int ToVector3(this int key)
    //{
    //    int x = (key & 0x3FF) - 512;
    //    int y = (key >> 0x0A & 0x3FF) - 512;
    //    int z = (key >> 0x14);
    //    Vector3 v3 = new Vector3(x, y, z);
    //    return v3;
    //}

    public static int ToInt (this Vector2 v)
    {
        return ((int)v.x) | (((int)v.y) << 0x0F);
    }

    public static int ToInt (this Vector2Int v)
    {
        return v.x | (v.y << 0x0F);
    }

    public static Vector2Int ToVector2Int(this int a_Key)
    {
        int y = a_Key >> 0x0F;
        int x = a_Key & 0x7FFF;
        return new Vector2Int(x, y);
    }
    public static Vector2 ToVector2(this int a_Key)
    {
        int y = a_Key >> 0x0F;
        int x = a_Key & 0x7FFF;
        return new Vector2(x, y);
    }

    public static int ToInt(this UnityEngine.Color32 v)
    {
        int value;
        value = ((v.r & 0xFF)
                | ((v.g & 0xFF) << 8)
                | ((v.b & 0xFF) << 16)
                | ((v.a & 0xFF) << 24)
                );
        return value;
    }
    public static UnityEngine.Color32 ToColor32(this int value)
    {
        int a = (value >> 24) & 0xFF;
        int b = (value >> 16) & 0xFF;
        int g = (value >> 8) & 0xFF;
        int r = (value & 0xFF);
        //转成byte的时候，自动会取得后面2个字节，即16B 256个字节，忽略前面一切数据
        return new Color32((byte)r, (byte)g, (byte)b, (byte)a);
    }
    public static int ToIntAlpha(this UnityEngine.Color32 v)
    {
        int value;
        value = ((v.r & 0xFF)
                | ((v.g & 0xFF) << 8)
                | ((v.b & 0xFF) << 16)
                | ((v.a & 0xFF) << 24)
                );
        return value;
    }
    public static UnityEngine.Color32 ToColor32Alpha(this int value)
    {
        int a = (value >> 24) & 0xFF;
        int b = (value >> 16) & 0xFF;
        int g = (value >> 8) & 0xFF;
        int r = (value & 0xFF);
        return new Color32((byte)r, (byte)g, (byte)b, (byte)a);
    }

    public static int ToInt(this UnityEngine.Color v)
    {
        int r = (int)(v.r * 0xFF);
        int g = (int)(v.g * 0xFF);
        int b = (int)(v.b * 0xFF);
        return b | ((g) << 0x8) | ((r) << 0x10);
    }

    public static UnityEngine.Color ToColor(this int v)
    {
        float r = v >> 0x10;
        float g = v >> 0x8 & 0xFF;
        float b = v & 0xFF;
        return new Color(r / 0xFF, g / 0xFF, b / 0xFF);
    }

    public static string FoString(this string str)
    {
        //string str = "//u4e2d//u6587";
        string outStr = "";
        if (!string.IsNullOrEmpty(str))
        {
            string[] strlist = str.Replace("//", "").Split('u');
            for (int i = 1; i < strlist.Length; i++)
            {
                //将unicode字符转为10进制整数，然后转为char中文字符  
                outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
            }
        }
        //Debuger.Log(outStr);
        return outStr;
    }
}
